package com.mfin.bs.mappers.impl;

import java.util.List;

import com.mfin.bom.interfaces.IDocCharge;
import com.mfin.bom.interfaces.ILoanPreference;
import com.mfin.vo.interfaces.IMappable;
import com.mfin.vo.loan.DocChargeListVO;
import com.mfin.vo.loan.DocChargeVO;
import com.mfin.vo.loan.LoanPreferenceVO;

public class LoanPreferenceMapper extends BaseMapper {

	@Override
	protected boolean doMap(IMappable fromArg, IMappable toArg) {
		boolean canPerformMap = false;

		if (fromArg instanceof ILoanPreference
				&& toArg instanceof LoanPreferenceVO) {
			canPerformMap = true;
			performMap((ILoanPreference) fromArg, (LoanPreferenceVO) toArg);
		} else if (fromArg instanceof LoanPreferenceVO
				&& toArg instanceof ILoanPreference) {
			performMap((LoanPreferenceVO) fromArg, (ILoanPreference) toArg);
			canPerformMap = true;
		} else if (fromArg instanceof DocChargeVO
				&& toArg instanceof IDocCharge) {
			performMap((DocChargeVO) fromArg, (IDocCharge) toArg);
			canPerformMap = true;
		} else if (fromArg instanceof IDocCharge
				&& toArg instanceof DocChargeVO) {
			performMap((IDocCharge) fromArg, (DocChargeVO) toArg);
			canPerformMap = true;
		}
		return canPerformMap;
	}

	private void performMap(LoanPreferenceVO fromArg, ILoanPreference toArg) {

		toArg.setInterestRate(fromArg.getInterestRate());
		toArg.setDuration(fromArg.getDuration());
	}

	private void performMap(ILoanPreference fromArg, LoanPreferenceVO toArg) {

		if (fromArg.getLoanMaster() != null) {
			toArg.setLoanType(fromArg.getLoanMaster().getLoanType());
		}
		toArg.setInterestRate(fromArg.getInterestRate());
		toArg.setDuration(fromArg.getDuration());

		List<IDocCharge> bomDocCharges = fromArg.getDocCharges();
		DocChargeVO[] docCharges = new DocChargeVO[bomDocCharges.size()];

		for (int index = 0; index < bomDocCharges.size(); index++) {
			docCharges[index] = new DocChargeVO();
			map(bomDocCharges.get(index), docCharges[index], false, false);
		}

		toArg.setDocChargeList(new DocChargeListVO(docCharges));
	}

	private void performMap(DocChargeVO fromArg, IDocCharge toArg) {
		toArg.setRangeMin(fromArg.getRangeMin());
		toArg.setRangeMax(fromArg.getRangeMax());
		toArg.setCharges(fromArg.getCharges());
	}

	private void performMap(IDocCharge fromArg, DocChargeVO toArg) {
		toArg.setRangeMin(fromArg.getRangeMin());
		toArg.setRangeMax(fromArg.getRangeMax());
		toArg.setCharges(fromArg.getCharges());
	}
}
